import {
    _decorator,
    Button,
    Component,
    director,
    Label,
    resources,
    Sprite,
    SpriteFrame,

} from 'cc';
import {showAlert} from '../alert/alert';
import {GameEvent} from '../../game-event';
import {UserFishFryTakeEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-take-event";
import {serverTimeHelper} from "db://assets/script/server-time-helper";
import {configsHelper} from "db://assets/script/configs-helper";
import {FishFryDetail, ImmortalDetail} from "db://assets/proto/shared/v1/wuni";
import {
    UserFishFryTakeCompleteEvent
} from "db://assets/script/common/events/fish-events/user-fish-fry-take-complete-event";
import {TalentHeadComp} from "db://assets/script/game/mainui/talent-head-comp";
import {UserFishCreateEvent} from "db://assets/script/common/events/fish-events/user-fish-create-event";
import Long from "long";
import {ChangeMainUiTagEvent} from "db://assets/script/common/events/ui-events/change-main-ui-tag-event";
import {UserFishAddOthersEvent} from "db://assets/script/common/events/fish-events/user-fish-add-others-event";
import {userInfo} from "db://assets/script/user-info";
import {
    UserFishFryRefuseCompleteEvent
} from "db://assets/script/common/events/fish-events/user-fish-fry-refuse-complete-event";
import {UserFishFryRefuseEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-refuse-event";

const {ccclass, property} = _decorator;

@ccclass('FishFryInfoComp')
export class FishFryInfoComp extends Component {

    @property(TalentHeadComp)
    head!: TalentHeadComp;

    @property(Label)
    nameLabel!: Label;

    @property(Sprite)
    genderIcon!: Sprite;

    @property(Label)
    ageLabel!: Label;

    @property(Label)
    talentLabel!: Label;

    @property(Label)
    hsLabel!: Label;

    @property(Label)
    hcLabel!: Label;

    @property(Label)
    leftEyeColors!: Label;

    @property(Label)
    rightEyeColors!: Label;

    @property(Label)
    skinLabel!: Label;

    @property(Button)
    checkBtn!: Button;

    @property(Button)
    refuseBtn!: Button;

    @property(Button)
    createBtn!: Button;

    fishId: Long | null = null


    start() {
    }

    init(fishFry: FishFryDetail) {
        this.fishId = fishFry.fishId;
        // fishFry.looks!.hairColor
        this.nameLabel.string = fishFry.fishName
        this.head.setHead(this.fishId, fishFry.gender, fishFry.talent, userInfo.loadUid())
        this.genderIcon.spriteFrame = resources.get('textures/ui-base/gender-' + fishFry!.gender + '-icon/spriteFrame', SpriteFrame)
        this.talentLabel.string = configsHelper.loadTalentName(fishFry.talent);
        this.talentLabel.color = configsHelper.loadTalentColor(fishFry!.talent);
        this.ageLabel.string = (Math.ceil((serverTimeHelper.loadServerTime() - (fishFry.birthAtMs.toNumber())) / 7200000)) + "岁"
        const hsc = configsHelper.loadHairStyle(fishFry.looks!.hairStyle)
        if (hsc) {
            this.hsLabel.string = hsc!.name
        } else {
            this.hsLabel.string = ''
        }
        const hcc = configsHelper.loadHairColor(fishFry.looks!.hairColor)
        if (hcc) {
            this.hcLabel.string = hcc!.name
        } else {
            this.hcLabel.string = ''
        }
        if (fishFry.looks!.leftEye == fishFry.looks!.rightEye) {
            this.rightEyeColors.node.active = false
            const eyeColor = configsHelper.loadEyeColor(fishFry.looks!.leftEye)
            if (eyeColor) {
                this.leftEyeColors.string = '瞳色：' + eyeColor!.name
            } else {
                this.leftEyeColors.string = '瞳色：'
            }
        } else {
            this.rightEyeColors.node.active = true
            this.leftEyeColors.node.active = true
            const leftEyeColor = configsHelper.loadEyeColor(fishFry.looks!.leftEye)
            if (leftEyeColor) {
                this.leftEyeColors.string = '左瞳色：' + leftEyeColor!.name
            } else {
                this.leftEyeColors.string = '左瞳色：'
            }
            const rightEyeColor = configsHelper.loadEyeColor(fishFry.looks!.rightEye)
            if (rightEyeColor) {
                this.rightEyeColors.string = '右瞳色：' + rightEyeColor!.name
            } else {
                this.rightEyeColors.string = '右瞳色：'
            }
        }
        const sc = configsHelper.loadSkinColor(fishFry.looks!.skin)
        if (sc) {
            this.skinLabel.string = sc!.name
        } else {
            this.skinLabel.string = ''
        }

        if (fishFry.liveIn.neq(-1)) {
            this.checkBtn.node.active = false
            this.createBtn.node.active = true
            this.refuseBtn.node.active = false
        } else {
            this.checkBtn.node.active = true
            this.createBtn.node.active = false
            this.refuseBtn.node.active = true
        }
        this.onEvent(fishFry);
    }

    onEvent(fishFry: FishFryDetail) {
        this.checkBtn.node.on(Button.EventType.CLICK, () => {
            showAlert("招收", "您确定消耗500「道韵」招收为「" + fishFry.fishName + "」外门弟子吗？", () => {
                director.emit(GameEvent.userFishFryRequestTake, new UserFishFryTakeEvent(this.fishId!.toString()))
            })
        })

        this.refuseBtn.node.on(Button.EventType.CLICK, () => {
            showAlert("劝返", "您确定劝返「" + fishFry.fishName + "」吗？", () => {
                director.emit(GameEvent.userFishFryRequestRefuse, new UserFishFryRefuseEvent(this.fishId!.toString()))
            })
        })

        this.createBtn.node.on(Button.EventType.CLICK, () => {
            showAlert("接迎", "您确定接迎「" + fishFry.fishName + "」为内门弟子吗？", () => {
                director.emit(GameEvent.userFishActionCreate, new UserFishCreateEvent(Long.fromNumber(-1), this.fishId!))
            })
        })
        director.on(GameEvent.userFishAdd, this.userFishAdd, this)
        director.on(GameEvent.userFishFryTakeComplete, this.takeComplete, this)
        director.on(GameEvent.userFishFryRefuseComplete, this.refuseComplete, this)
    }

    offEvent() {
        director.off(GameEvent.userFishAdd, this.userFishAdd, this)
        director.off(GameEvent.userFishFryTakeComplete, this.takeComplete, this)
        director.off(GameEvent.userFishFryRefuseComplete, this.refuseComplete, this)
    }

    userFishAdd(fish: ImmortalDetail) {
        if (fish.fish?.id.eq(this.fishId!)) {
            const params: { [key: string]: any } = {}
            params['fish-id'] = this.fishId
            director.emit(GameEvent.uiChangeMainUiTag, new ChangeMainUiTagEvent('battlefield', params))
            this.node.destroy()
        }
    }


    takeComplete(event: UserFishFryTakeCompleteEvent) {
        if (event.fishFry.fishId.eq(this.fishId!)) {
            this.node.destroy()
        }
    }


    refuseComplete(event: UserFishFryRefuseCompleteEvent) {
        if (this.fishId?.eq(event.fishId)) {
            this.node.destroy()
        }
    }


    protected onDestroy(): void {
        this.offEvent()
    }

    update(_: number) {

    }
}


